<template>
    <div class="curd">
        <el-input style="width:200px" v-model="name" class="input-box m-2" placeholder="时间" :prefix-icon="Calendar"></el-input>
        <el-input style="width:200px" v-model="name" placeholder="名称" :suffix-icon="Search"></el-input>
        <el-button type="primary" @click="search" style="margin-left:30px" :icon="Search">search</el-button>
        <el-button type="primary" plain @click="handleAdd">add</el-button>
        <el-button type="danger" plain @click="handleDel" :icon="Delete">del</el-button>


        <div style="margin-top: 10px;">
            <el-table :data="tableData" border stripe  style="width: 100%;">
                <el-table-column type="selection" width="55" />
                <el-table-column prop="date" label="Date" width="150" />
                <el-table-column prop="name" label="Name" width="120" />
                <el-table-column prop="state" label="State" width="120" />
                <el-table-column prop="address" label="Address" width="600"/>
                <el-table-column label="操作">
                  <template #default="scope">
                    <el-button type="primary" size="small" @click="handleEdit(scope.row,scope.$index)">edit</el-button>
                    <el-button type="danger" size="small" @click="remove(scope.$index)">del</el-button>
                  </template>
                </el-table-column>
            </el-table>
        </div>
    </div>
    <!-- 拟态框 -->
    <el-dialog v-model="dialogFormVisible" :title='title' width="40%">
      <el-form :model="ruleForm" ref="ruleFormRef" :rules="rules">
        <el-form-item label="名称" prop="name">
          <el-input v-model="ruleForm.name" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="ruleForm.state" placeholder="请选择">
            <el-option label="待完成" value="1"></el-option>
            <el-option label="已完成" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="ruleForm.address" placeholder="请输入" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="时间" prop="date">
            <el-date-picker v-model="ruleForm.date" type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
                placeholder="请选择" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogFormVisible = false">Cancel</el-button>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          Confirm
        </el-button>
      </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import { Calendar, Search, Delete } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
let title = ref('添加')
let dialogFormVisible = ref(false)
let ruleForm = ref({
  name: '',
  state: '',
  address: '',
  date: '',
})
const ruleFormRef = ref<FormInstance>()
const name = ref('')

// 添加弹框
const handleAdd = () => {
  title.value = '添加'
  dialogFormVisible.value = true
  ruleForm = ref({
    name: '',
    state: '',
    city: '',
    date: '',
  })
}
// 保存
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid) => {

    if (valid) {
      if(title.value === '添加') {
        tableData.value.push(ruleForm.value)
      }else if(title.value === '编辑') {
        tableData.value[globalIndex.value] = ruleForm.value
      }
    }
  })

  dialogFormVisible.value = false
}

// 修改弹框
const globalIndex = ref(-1)
const handleEdit = (row: any, index: number) => {
  globalIndex.value = index
  title.value = '编辑'
  ruleForm.value = row
  dialogFormVisible.value = true
}

// 删除
const remove = (index) => {
  tableData.value.splice(index, 1)
}
const handleDel = () => {

}
// 查询
const search = () => {
  tableData.value = tableData.value.filter(v => v.name.includes(name.value))
}

const rules = reactive({
  name: [
    { required: true, message: 'Please input Activity name', trigger: 'blur' },
  ],
  state: [
    { required: true, message: 'Please input Activity state', trigger: 'blur' },
  ],
  address: [
    { required: true, message: 'Please input Activity address', trigger: 'blur' },
  ],
  date: [
    { required: true, message: 'Please input Activity date', trigger: 'blur' },
  ],
})
const tableData = ref([
    {
        date: '2016-05-03',
        name: 'Tom',
        state: 'California',
        address: 'Los Angeles',
    },
    {
        date: '2016-05-02',
        name: 'Tom',
        state: 'California',
        address: 'Los Angeles',
    },
])
</script>

<style lang="less" scoped>
.curd {
    margin: 10px;
}
</style>